package cn.fanxc.fogandhaze.controller;

import cn.fanxc.Model.DATA;
import cn.fanxc.Model.FH;
import cn.fanxc.Model.T7;
import cn.fanxc.Model.ZL;
import cn.fanxc.fogandhaze.service.Service;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @ClassName Controller
 * @Description 请求controllere
 * @Author Fanxc
 * @DATE 2021/5/3 21:06
 * @Version 1.0
 */
@org.springframework.stereotype.Controller

public class Controller {


    @Autowired
    private Service service;

    @RequestMapping("/test")
    public String get(Model model){
        FH fh=new FH();
        fh.setWea("多云转小雨");
        model.addAttribute("fh",fh);
        return "index5";
    }

    @RequestMapping(value = "/aq",method = RequestMethod.GET)
    public String getUrbanAirQuality(@RequestParam(value = "city") String city, Model model)
    {
        JSONObject air = service.getUrbanAirQuality(city);
        JSONObject airQuality = service.getAirQuality(city);
        JSONObject temWeek = service.getT7(city);

        if (air==null||airQuality==null||temWeek==null)return "error";

        FH fh = (FH) JSONObject.toBean(air, FH.class);
        ZL zl = (ZL) JSONObject.toBean(airQuality, ZL.class);
        T7 t7 = (T7) JSONObject.toBean(temWeek, T7.class);

        ArrayList<String> list1=new ArrayList<>();
        ArrayList<String> list2=new ArrayList<>();
        ArrayList<String> list3=new ArrayList<>();
        ArrayList<String> list4=new ArrayList<>();
        for (DATA data :
                t7.getData()) {
            list1.add(data.getDate());
            list2.add(data.getWea());
            list3.add(data.getTem_day());
            list4.add(data.getTem_night());
        }
        model.addAttribute("fh",fh);
        model.addAttribute("zl",zl);
        model.addAttribute("t7",t7);
        model.addAttribute("list1",list1);
        model.addAttribute("list2",list2);
        model.addAttribute("list3",list3);
        model.addAttribute("list4",list4);

        System.out.println(fh.toString());
        System.out.println(zl.toString());
        System.out.println(t7.toString());
        System.out.println(list1.toString());
        System.out.println(list2.toString());
        System.out.println(list3.toString());
        System.out.println(list4.toString());
        return "details";
    }

}
